package com.qianbei.common.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.utilis.img.CropCircleTransformation;

/**
 * Created by Administrator on 2015/1/26.
 */
public class ViewHolder {


    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;
    private Context mContext;
    private CropCircleTransformation mCropCircleTransformation;

    private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.mContext = context;
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        // setTag
        mConvertView.setTag(this);
        mCropCircleTransformation = new CropCircleTransformation(mContext);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId,
            int position) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        }
        return (ViewHolder) convertView.getTag();
    }

    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为TextView设置富文本字符串
     *
     * @param viewId
     * @param text 富文本
     * @return
     */
    public ViewHolder setText(int viewId, Spannable text) {
        TextView view = getView(viewId);
        view.setText(text);
        view.setMovementMethod(LinkMovementMethod.getInstance());
        return this;
    }

    /**
     * 设置View显示与隐藏
     *
     * @param viewId
     * @param visibility
     * @return
     */
    public ViewHolder setVisibility(int viewId, int visibility) {
        View view = getView(viewId);
        view.setVisibility(visibility);
        return this;
    }


    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param textId
     * @return
     */
    public ViewHolder setText(int viewId, int textId) {
        return setText(viewId,mContext.getString(textId));
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param bm
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }


    /**
     * 加载网络图片
     * @param viewId
     * @param placeHolder
     * @param url
     * @return
     */
    public ViewHolder setImageBitmap(int viewId,int placeHolder,String url){
        ImageView view = getView(viewId);
        if(!((BaseActivity) mContext).isFinishing()){
            Glide.with(mContext).load(url).placeholder(placeHolder).into(view);
        }

        return this;
    }

    public ViewHolder setImageBitmap(int viewId,String url){
        ImageView view = getView(viewId);
        if(!((BaseActivity) mContext).isFinishing()){
            Glide.with(mContext).load(url).placeholder(R.drawable.kh_img_default).into(view);
        }

        return this;
    }

    /**
     * 加载网络图片并裁剪成圆角
     * @param viewId
     * @param placeHolder
     * @param url
     * @return
     */
    public ViewHolder setImageBitmapCircle(int viewId,int placeHolder,String url){
        ImageView view = getView(viewId);

        if(!((BaseActivity) mContext).isFinishing()){
            Glide.with(mContext)
                    .load(url)
                    .placeholder(placeHolder)
                    .bitmapTransform(mCropCircleTransformation)
                    .into(view);

        }

        return this;
    }


    /**
     * 加载网络图片并裁剪成圆角
     * @param viewId
     * @param url
     * @return
     */
    public ViewHolder setImageBitmapCircle(int viewId,String url){
        ImageView view = getView(viewId);

        if(!((BaseActivity) mContext).isFinishing()){
            Glide.with(mContext)
                    .load(url)
                    .placeholder(R.drawable.ic_launcher)
                    .bitmapTransform(mCropCircleTransformation)
                    .into(view);

        }

        return this;
    }

    /**
     * 加载网络图片并裁剪成圆角
     * @param viewId
     * @param placeHolder
     * @param resId 资源中的drawable
     * @return
     */
    public ViewHolder setImageBitmapCircle(int viewId,int placeHolder,int resId){
        ImageView view = getView(viewId);

        if(!((BaseActivity) mContext).isFinishing()){
            Glide.with(mContext)
                    .load(resId)
                    .placeholder(placeHolder)
                    .bitmapTransform(mCropCircleTransformation)
                    .into(view);

        }

        return this;
    }


    public ViewHolder setBackgroundResource(int viewId, int bgId){
        View view = getView(viewId);
        view.setBackgroundResource(bgId);
        return this;
    }

    public int getPosition() {
        return mPosition;
    }
}
